package com.glory.huaweipda.viewmodel

import androidx.databinding.ObservableArrayList
import com.flyco.tablayout.listener.CustomTabEntity
import com.flyco.tablayout.listener.OnTabSelectListener
import com.glory.huaweipda.BR
import com.glory.huaweipda.R
import com.glory.huaweipda.bean.AreaEqpInfoBean
import com.glory.huaweipda.model.TaskRepository
import com.glory.mvvmnet.base.BaseViewModel
import com.glory.mvvmnet.entity.CallObserveBean
import com.google.gson.JsonObject
import me.tatarka.bindingcollectionadapter2.ItemBinding
import java.util.*

class EquipmentModel : BaseViewModel() {
    var mfgItems = ObservableArrayList<AreaEqpInfoBean>()
    val EQUIPMENT = 0
    var titles = arrayOf("全部","DOWN", "PM", "IDLE", "RUN","DEBUG","PFRM","QUAL","ASIDE")
    var mTabEntities = ObservableArrayList<CustomTabEntity>()
    var areaEqpInfoBean : ArrayList<AreaEqpInfoBean> = arrayListOf()
    private val repository by lazy { TaskRepository() }

    private val mainItemClick = object : OnItemClickListener {
        override fun onItemClick(item: Any) {
//            defUI.callObserve.value = CallObserveBean(EQUIPMENT, item)
        }
    }
    var tabOnClickListener = object : OnTabSelectListener {
        override fun onTabSelect(position: Int) {
            initAssets(titles[position])
        }

        override fun onTabReselect(position: Int) {

        }
    }
    var itemBinding = ItemBinding.of<AreaEqpInfoBean>(BR.areaEqpInfoBean, R.layout.item_equipment)
    fun initTab() {
        if (mTabEntities.size == 0) {
            titles.forEach {
                mTabEntities.add(object : CustomTabEntity {
                    override fun getTabTitle(): String {
                        return it
                    }

                    override fun getTabSelectedIcon(): Int {
                        return 0
                    }

                    override fun getTabUnselectedIcon(): Int {
                        return 0
                    }
                })
            }
        }
    }


    fun initAssets(string:String) {

        mfgItems.clear()

        for (i in areaEqpInfoBean) {
            when(string){
                "全部"->{
                    mfgItems.add(i)
                }
                "DOWN"->{
                    if (i.equipmentStatusPMS.equals("DOWN")){
                        mfgItems.add(i)
                    }
                }
                "PM"->{
                    if (i.equipmentStatusPMS.equals("PM")){
                        mfgItems.add(i)
                    }
                }
                "IDLE"->{
                    if (i.equipmentStatusPMS.equals("IDLE")){
                        mfgItems.add(i)
                    }
                }
                "RUN"->{
                    if (i.equipmentStatusPMS.equals("RUN")){
                        mfgItems.add(i)
                    }
                }
                "DEBUG"->{
                    if (i.equipmentStatusPMS.equals("DEBUG")){
                        mfgItems.add(i)
                    }
                }
                "PFRM"->{
                    if (i.equipmentStatusPMS.equals("PFRM")){
                        mfgItems.add(i)
                    }
                }
                "QUAL"->{
                    if (i.equipmentStatusPMS.equals("QUAL")){
                        mfgItems.add(i)
                    }
                }
                "ASIDE"->{
                    if (i.equipmentStatusPMS.equals("ASIDE")){
                        mfgItems.add(i)
                    }
                }
            }

        }

        defUI.callObserve.value = CallObserveBean(2)
    }

    fun defaultGetAreaEqpInfo(areaName: String) {
        val map = JsonObject()
        map.addProperty("AreaName", areaName)

        launchOnlyResult({
            val defaultGetAreaEqpInfo = repository.defaultGetAreaEqpInfo(
                map)
            defUI.callObserve.value = CallObserveBean(1, defaultGetAreaEqpInfo)
        })
    }

    interface OnItemClickListener {
        fun onItemClick(item: Any)
    }

}